package com.jxb.dialog.utils;


import android.content.Context;

import androidx.appcompat.widget.AppCompatTextView;

import com.blankj.utilcode.util.KeyboardUtils;
import com.jxb.dialog.BaseDialog;
import com.jxb.dialog.R;

/**
 * @author jinxb
 * date     : 2021/5/7
 * desc     :
 */
public class InfoDialogUtils {

    private InfoDialogUtils(){

    }

    public static InfoDialogUtils getInstance(){
        return Holder.sInstance;
    }

    private static class Holder{
        private static final InfoDialogUtils sInstance = new InfoDialogUtils();
    }

    private InfoDialog infoDialog;

    public void showInfoDialog(Context context, String content) {
        showInfoDialog(context, "提示", content, null);
    }

    public void showInfoDialog(Context context, String title, String content) {
        showInfoDialog(context, title, content, null);
    }


    public void showInfoDialog(Context context, String content,
                               BaseDialog.OnBtnClickListener onConfirmListener) {
        showInfoDialog(context, "提示", content, onConfirmListener);
    }

    public void showInfoDialog(Context context, String title, String content,
                               BaseDialog.OnBtnClickListener onConfirmListener) {
        infoDialog = new InfoDialog(context, title, content, onConfirmListener);
        infoDialog.show();
    }

    public void dismiss() {
        if (infoDialog != null && infoDialog.isShowing()) {
            infoDialog.dismiss();
        }
    }

    private class InfoDialog extends BaseDialog {
        private String title = "";
        private String content = "";
        private OnBtnClickListener onConfirmListener;
        private AppCompatTextView titleTextView;
        private AppCompatTextView confirmTextView;
        private AppCompatTextView contentTextView;

        public InfoDialog(Context context, String title, String content,
                          OnBtnClickListener onConfirmListener) {
            super(context);
            this.title = title;
            this.content = content;
            this.onConfirmListener = onConfirmListener;
        }

        @Override
        protected int getContentView() {
            return R.layout.dialog_info;
        }

        @Override
        public void initViews() {
            titleTextView = findViewById(R.id.titleTextView);
            contentTextView = findViewById(R.id.contentTextView);
            confirmTextView = findViewById(R.id.confirmTextView);
        }

        @Override
        public void initListeners() {
            confirmTextView.setOnClickListener(view1 -> {
                KeyboardUtils.hideSoftInput(view1);
                if (onConfirmListener != null) {
                    onConfirmListener.onClick();
                }
                dismiss();
            });
        }

        @Override
        public void show() {
            super.show();
            titleTextView.setText(title);
            contentTextView.setText(content);
        }
    }
}
